﻿using System.Collections.Specialized;
using System.Windows;
using WikiMarkupConverter.Properties;
using System.Linq;

namespace WikiMarkupConverter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {

        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;


        }

        #region Settings Management
        /// <summary>
        /// Restore saved user settings
        /// </summary>
        private void RestoreSettings()
        {
            // Restoring Extension list
            if (Settings.Default.Extensions == null) return;

            foreach (var extension in Settings.Default.Extensions)
            {
                LbSettingsExtensions.Items.Add(extension);
            }

            // Restoring last origin and target syntax
            CbOrigin.SelectedIndex = CbOrigin.Items.IndexOf(Settings.Default.LastOrigin);
            CbTarget.SelectedIndex = CbTarget.Items.IndexOf(Settings.Default.LastTarget);

            //Restoring last input and output path
            TxbInputPath.Text = Settings.Default.LastInputPath;
            TxbOutputPath.Text = Settings.Default.LastOutputPath;

        }

        /// <summary>
        /// Save user settings
        /// </summary>
        private void SaveSettings()
        {
            // Saving extension list
            var strCollection = new StringCollection();
            strCollection.AddRange(LbSettingsExtensions.Items.OfType<string>().ToArray());
            Settings.Default.Extensions = strCollection;

            // Saving origin and target syntax
            if (CbOrigin.SelectedItem != null)
                Settings.Default.LastOrigin = CbOrigin.SelectedItem.ToString();
            if (CbTarget.SelectedItem != null)
                Settings.Default.LastTarget = CbTarget.SelectedItem.ToString();

            // Saving input and ouput path
            Settings.Default.LastInputPath = TxbInputPath.Text;
            Settings.Default.LastOutputPath = TxbOutputPath.Text;

            Settings.Default.Save();
        }
        #endregion

        #region Window Events

        private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            ChangeProcessMode(true);

            var syntaxesList = (await XmlFileHelper.GetSyntaxesAsync()).Select(s => s.Name).ToList();

            CbOrigin.ItemsSource = syntaxesList;
            CbTarget.ItemsSource = syntaxesList;

            RestoreSettings();

            ChangeProcessMode(false);
        }

        private async void BtnConvert_OnClick(object sender, RoutedEventArgs e)
        {
            ChangeProcessMode(true);

            // Getting original and target syntax
            var origin = (await XmlFileHelper.GetSyntaxesAsync()).Find(s => s.Name == CbOrigin.SelectedItem.ToString());
            var target = (await XmlFileHelper.GetSyntaxesAsync()).Find(s => s.Name == CbTarget.SelectedItem.ToString());

            // Getting extensions
            var exts = LbSettingsExtensions.Items.OfType<string>().ToList();

            // Processing convertion
            var syntaxConverter = new SyntaxConverter(origin, target);
            await syntaxConverter.ProcessConvertAsync(TxbInputPath.Text, TxbOutputPath.Text, exts);

            ChangeProcessMode(false);
        }

        private void BtnSettingsRemoveExtension_OnClick(object sender, RoutedEventArgs e)
        {
            while (LbSettingsExtensions.SelectedItem != null)
            {
                LbSettingsExtensions.Items.Remove(LbSettingsExtensions.SelectedItem);
            }
        }

        private void BtnSettingsAddExtension_OnClick(object sender, RoutedEventArgs e)
        {
            var input = TxbSettingsExtension.Text;
            if (string.IsNullOrWhiteSpace(input) || !input.StartsWith("."))
                return;

            if (!LbSettingsExtensions.Items.Contains(input))
                LbSettingsExtensions.Items.Add(input);

            TxbSettingsExtension.Clear();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            SaveSettings();

            base.OnClosing(e);
        }

        private void BtnBrowseInputPath_OnClick(object sender, RoutedEventArgs e)
        {
            var path = FileHelper.GetDirectory(TxbInputPath.Text);
            if (string.IsNullOrWhiteSpace(path))
                return;

            TxbInputPath.Text = path;
        }

        private void BtnBrowseOuputPath_Click(object sender, RoutedEventArgs e)
        {
            var path = FileHelper.GetDirectory(TxbInputPath.Text);
            if (string.IsNullOrWhiteSpace(path))
                return;

            TxbOutputPath.Text = path;
        }
        #endregion

        #region Private Helpers
        /// <summary>
        /// Change the state of the window to working or idle mode.
        /// Working mode will turn the progress bar ON and the buttons OFF. 
        /// </summary>
        private void ChangeProcessMode(bool isWorking)
        {
            PgbMain.IsIndeterminate = isWorking;
            BtnConvert.IsEnabled = !isWorking;
        }
        #endregion




    }
}
